package com.example.last.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.last.common.AutoLog;
import com.example.last.common.CaptureConfig;
import com.example.last.common.Result;
import com.example.last.common.enums.ErrorCode;
import com.example.last.common.enums.RoleEnum;
import com.example.last.entity.Account;
import com.example.last.exception.CustomException;
import com.example.last.service.AdminService;
import com.example.last.service.DoctorService;
import com.example.last.service.UserService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RequestMapping()
@RestController
public class WebController {
    @Resource
    private AdminService adminService;
    @Resource
    private DoctorService doctorService;
    @Resource
    private UserService userService;
    @AutoLog("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody Account account, @RequestParam String key, HttpServletRequest request){
        //用户判空
        if(account.getUsername() == null || "" .equals(account.getUsername())){
            throw new CustomException(ErrorCode.USER_NOT_ERROR);
        }
        //密码判空
        if(account.getPassword() == null || "" .equals(account.getPassword())){
            throw new CustomException(ErrorCode.PASSWORD_NOT_ERROR);
        }
        //验证码非空判断
        String verCode = account.getVerCode();
        if (verCode == null || verCode.isEmpty()) {
            throw new CustomException(ErrorCode.CAPTURE_ERROR);
        }
        //判断验证码
        if(!account.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))){
            CaptchaUtil.clear(request);
            return Result.error("验证码错误");
        }
        Account loginAccount = null;
        if(RoleEnum.ADMIN.name().equals(account.getRole())){
            loginAccount = adminService.login(account);
        }
        if(RoleEnum.DOCTOR.name().equals(account.getRole())){
            loginAccount = doctorService.login(account);
        }
        if(RoleEnum.USER.name().equals(account.getRole())){
            loginAccount = userService.login(account);
        }
        CaptureConfig.CAPTURE_MAP.remove(key);
        return Result.success(loginAccount);
    }

    //注册功能
    @PostMapping("/register")
    public Result register(@RequestBody Account account){
        //用户判空
        if(account.getUsername() == null || "" .equals(account.getUsername())){
            throw new CustomException(ErrorCode.USER_NOT_ERROR);
        }
        //密码判空
        if(account.getPassword() == null || "" .equals(account.getPassword())){
            throw new CustomException(ErrorCode.PASSWORD_NOT_ERROR);
        }
        if(!account.getPassword().equals(account.getConfirmPassword())){
            throw new CustomException(ErrorCode.PASSWORD_TWO_NOT);
        }
        //角色
        if(account.getRole() == null || "" .equals(account.getRole())){
            throw new CustomException(ErrorCode.ROLE_NOT_EXIST);
        }

        if(RoleEnum.DOCTOR.name().equals(account.getRole())) {
            doctorService.register(account);
        }
        if(RoleEnum.USER.name().equals(account.getRole())) {
            userService.register(account);
        }
        return Result.success();
    }
    @PutMapping("updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if(StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword()) || ObjectUtil.isEmpty(account.getNewPassword())){
            throw new CustomException(ErrorCode.PARAM_ERROR);
        }
        if(RoleEnum.ADMIN.name().equals(account.getRole())){
            adminService.updatePassword(account);
        }
        if(RoleEnum.DOCTOR.name().equals(account.getRole())){
            doctorService.updatePassword(account);
        }
        if(RoleEnum.USER.name().equals(account.getRole())){
            userService.updatePassword(account);
        }
        return Result.success();
    }

}
